<template>
  <div>
    <a-modal
      v-model="typeChildData.show"
      width="800px"
      style="overflow:hidden"
      @cancel="handleClose"
    >
      <div slot="title">
        <span>{{typeChildData.title}}</span>
      </div>
          <el-form
            :model="parames"
            :rules="rules"
            label-position="left"
            ref="parames"
            label-width="130px"
            class="addForm"
            size="small"
          >
            <el-row :gutter="40" style="padding: 30px">
              <el-col :span="24">
                <el-form-item label="行业名称:" prop="industryName">
                  <el-input size="small" v-model="parames.industryName" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="小规模纳税人税率:">
                  <el-input size="small" v-model="parames.smallScaleTaxRate" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="一般纳税人税率:">
                  <el-input size="small" v-model="parames.generalTexRate" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="行业资讯:">
                  <el-input size="small" v-model="parames.industryInformation" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="经营范围/行业资质许可证:" label-width="180px">
                  <el-button size="small" type="primary" @click="handleAdd">添加</el-button>
                </el-form-item>
                <el-form-item label-width="0">
                  <el-table
                    :data="parames.businessScopeList"
                    border
                    style="margin-top:14px;">
                    <el-table-column label="经营范围">
                      <template slot-scope="scope">
                        <el-input size="small" v-model="scope.row.businessScopeName"></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="行业资质许可证">
                      <template slot-scope="scope">
                        <el-input size="small" v-model="scope.row.industryQualificationLicense"></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作" width="60" >
                      <template slot-scope="scope">
                        <el-button size="small" type="text" @click="handleDel(scope.$index,scope.row)">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
                <el-form-item label="经典案例:" label-width="80px">
                  <el-input size="small" v-model="customerName" style="width:180px" placeholder="请输入客户名称"></el-input>
                  <el-button size="small" type="primary" @click="handleAdd2">添加</el-button>
                </el-form-item>
                <el-form-item label-width="0">
                  <el-table
                    :data="parames.successCaseList"
                    border
                    style="margin-top:14px;">
                    <el-table-column label="客户名称" prop="customerName" show-overflow-tooltip></el-table-column>
                    <el-table-column label="主营行业" prop="industryName"></el-table-column>
                    <el-table-column label="注册地">
                      <template slot-scope="scope">
                        <span>{{scope.row.area}}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="注册资金(万元)" prop="registeredCapital"></el-table-column>
                    <el-table-column label="操作" width="60" >
                      <template slot-scope="scope">
                        <el-button size="small" type="text" @click="handleDel2(scope.$index,scope.row)">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
              </el-col> 
            </el-row>
          </el-form>
          <template slot="footer">
            <el-button size="small" type="primary" :loading="loadingBtn" @click="handleSend">保存</el-button>
            <el-button size="small" @click="handleClose">取消</el-button>
          </template>
    </a-modal>
  </div>
</template>

<script>
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'
export default {
  props: ["typeChildData"],
  data() {
    return {
      loadingBtn: false,
      customerName: '',
      parames: {
        id: '',
        parentId: '', //父行业id
        industryName: '', //行业名称
        smallScaleTaxRate: '',//小规模纳税人税率,
        generalTexRate: '',//一般纳税人税率,
        industryInformation: '',//行业资讯,
        businessScopeList: [
            // businessScopeName:经营范围,
            // industryQualificationLicense:行业资质许可证
        ],
        successCaseList: []
      },
      menuType: [],
      rules: {
        industryName: [
          { required: true, message: "必填", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    handleAdd () {
      this.parames.businessScopeList.push({
        businessScopeName: '',
        businessScopeList: ''
      })
    },
    handleAdd2 () { //查询客户的信息
      if (!this.customerName) {
        this.$message.error('请输入客户名称');
        return false
      }
      this.$post(this.$api.postCustomerBaseInfoSuccessCaseList,{
        customerName: this.customerName
      }).then(response => {
          if (response.data.succeed) {
            let data = response.data.data
            if (data.length == 0) {
              this.$message.error('当前搜索没有数据，请重新搜索！');
            }
            data.forEach(el=>{
              // if (el.customerName && el.industryName) {
                if (el.administrativeRegion) {
                  let area = el.administrativeRegion
                  el.area = CodeToText[Number(area[0])] + '-' + CodeToText[Number(area[1])] + '-' + CodeToText[Number(area[2])]
                }
                this.parames.successCaseList.push(el)
                this.parames.successCaseList = this.$utils.unique2(this.parames.successCaseList,'customerName')
              // }
            })
          }
        })
    },
    handleDel (index,row) {
      this.parames.businessScopeList.splice(index,1)
    },
    handleDel2 (index,row) {
      this.parames.successCaseList.splice(index,1)
    },
    handleClose() {
      //关闭弹窗
      this.typeChildData.show = false;
    },
    handleSend () { //提交信息
      this.$refs['parames'].validate((valid) => {
        if (valid) {
          this.loadingBtn = true
          if (this.typeChildData.type == 'newP') { //添加子科目
            this.parames.parentId = this.typeChildData.id
            this.$post(this.$api.postCustomerBaseInfoIndustryAdd,this.parames).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          } else if (this.typeChildData.type == 'editP') { //修改子科目
            this.$post(this.$api.postCustomerBaseInfoIndustryEdit,this.parames).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          }
          setTimeout(() => {
            this.loadingBtn = false
          }, 300);
        }
      })
    },
    render () {
      if (this.typeChildData.type == 'editP') {
        this.parames.industryName = this.typeChildData.name
        this.$post(this.$api.postCustomerBaseInfoIndustryFind,{
          industryId: this.typeChildData.id
        }).then(response => {
          if (response.data.succeed) {
            let data = response.data.data
            this.parames.id = data.id
            this.parames.parentId = data.parentId //父行业id
            this.parames.industryName = data.industryName //行业名称
            this.parames.smallScaleTaxRate = data.smallScaleTaxRate//小规模纳税人税率
            this.parames.generalTexRate = data.generalTexRate//一般纳税人税率
            this.parames.industryInformation = data.industryInformation//行业资讯,
            this.parames.businessScopeList = data.businessScopeList
            data.successCaseList.forEach(el=>{
              if (el.administrativeRegion) {
                let area = el.administrativeRegion
                el.area = CodeToText[Number(area[0])] + '-' + CodeToText[Number(area[1])] + '-' + CodeToText[Number(area[2])]
              }
            })
            this.parames.successCaseList = data.successCaseList
          }
        })
      }
    },
    restData () {
        this.parames.id = ''
        this.parames.industryName = ''
        this.parames.parentId = ''
        this.parames.smallScaleTaxRate = ''//小规模纳税人税率,
        this.parames.generalTexRate = ''//一般纳税人税率,
        this.parames.industryInformation = ''//行业资讯,
        this.parames.businessScopeList = []
        this.parames.successCaseList = []
        this.customerName = ''
        this.$refs.parames.resetFields()
    }
  },
  computed: { //计算属性
    changeDialog () {
      return this.typeChildData.show
    }
  },
  watch: {
    changeDialog: {
      handler (curVal, oldVal) {
        if (curVal) {
          this.render()
        } else {
          this.restData()
        }
      }
    },
    deep: true
  }
};
</script>

<style scoped>
</style>
